package com.ibeeking.gmc.global.g.rest.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.common.config.prop.FileOssProp;
import com.ibeeking.found.common.entity.TenantDetailDTO;
import com.ibeeking.found.common.utils.BlobUtils;
import com.ibeeking.gmc.global.api.common.param.TenantDetailParam;
import com.ibeeking.gmc.global.g.rest.service.ITenantDetailService;
import com.ibeeking.gmc.global.service.common.dos.TenantDetailDO;
import com.ibeeking.gmc.global.service.common.vo.TenantDetailVO;
import com.ibeeking.gmc.global.service.mapper.TenantDetailMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.id.IdGenerator;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.sql.Blob;
import java.util.List;

/**
 * @ClassName TenantLegalInfoServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2020-12-24 16:34
 **/
@Service
public class TenantDetailServiceImpl extends ServiceImpl<TenantDetailMapper, TenantDetailDO> implements ITenantDetailService {

    @Resource
    private TenantDetailMapper tenantDetailMapper;

    @Override
    public TenantDetailVO view(Long id) {
        TenantDetailDO entityDO = tenantDetailMapper.selectById(id);
        if (entityDO == null) {
            return null;
        }
        TenantDetailVO entityVO = BeanUtil.convertBean(entityDO, TenantDetailVO.class);
        Object obj = entityDO.getDetail();
        if(obj != null) {
            entityVO.setDetail(new String((byte[])obj, StandardCharsets.UTF_8));
        }
        return entityVO;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean del(Long id) {
        return tenantDetailMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delAllByTenantId(Integer tenantId) {
        List<TenantDetailVO> vos = this.listByTenantId(tenantId);
        if (vos != null && vos.size() > 0) {
            return tenantDetailMapper.delete(new LambdaQueryWrapper<TenantDetailDO>().eq(TenantDetailDO::getTenantId, tenantId)) >0;
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delByTenantId(Integer type, Integer tenantId) {
        TenantDetailVO vo = this.viewByTenantId(type, tenantId);
        if (vo == null) {
            throw new BusinessException("请求资源失败");
        }
        return this.del(vo.getId());
    }
    @Override
    public List<TenantDetailVO> listByTenantId(Integer tenantId) {
        List<TenantDetailDO> entityDetailDOS = this.tenantDetailMapper.selectList(new LambdaQueryWrapper<TenantDetailDO>()
                .eq(TenantDetailDO::getTenantId, tenantId));
        if(Collections3.isEmpty(entityDetailDOS)) return null;
        return JsonUtils.jsonToList(entityDetailDOS, TenantDetailVO.class);
    }

    @Override
    public TenantDetailVO viewByTenantId(Integer type, Integer tenantId) {
        QueryWrapper<TenantDetailDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TenantDetailDO::getType, type);
        queryWrapper.lambda().eq(TenantDetailDO::getTenantId, tenantId);
        TenantDetailDO entityDO = this.tenantDetailMapper.selectOne(queryWrapper);
        if (entityDO == null) {
            return null;
        }
        TenantDetailVO entityVO = BeanUtil.convertBean(entityDO, TenantDetailVO.class);
        Object obj = entityDO.getDetail();
        if(obj != null) {
            entityVO.setDetail(new String((byte[])obj, StandardCharsets.UTF_8));
        }
        return entityVO;
    }

    @Override
    public TenantDetailDTO queryDetailByTenantId(Integer type, Integer tenantId) {
        QueryWrapper<TenantDetailDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TenantDetailDO::getType, type);
        queryWrapper.lambda().eq(TenantDetailDO::getTenantId, tenantId);
        TenantDetailDO entityDO = this.tenantDetailMapper.selectOne(queryWrapper);
        if (entityDO == null) {
            return null;
        }
        TenantDetailDTO entityDTO = BeanUtil.convertBean(entityDO, TenantDetailDTO.class);
        Object obj = entityDO.getDetail();
        if(obj != null) {
            entityDTO.setDetail(new String((byte[])obj, StandardCharsets.UTF_8));
        }
        return entityDTO;
    }

    @Override
    public Boolean save(TenantDetailParam param) {
        Integer tenantId = param.getTenantId();
        if(tenantId == null) throw new BusinessException("租户Id不能为空");
        Blob blobDetailContent = null;
        String detailContent = param.getDetail();
        if(StringUtils.isNotEmpty(detailContent)) {
            blobDetailContent = BlobUtils.string2blob(detailContent);
        }
        TenantDetailDO entityDO = this.tenantDetailMapper.selectOne(new LambdaQueryWrapper<TenantDetailDO>()
                .eq(TenantDetailDO::getType, param.getType())
                .eq(TenantDetailDO::getTenantId, tenantId));
        if(entityDO == null){
            entityDO = new TenantDetailDO();
            entityDO.setId(IdGenerator.getId());
            entityDO.setType(param.getType());
            entityDO.setTenantId(tenantId);
            entityDO.setDetail(blobDetailContent);
            return this.tenantDetailMapper.insert(entityDO) > 0;
        }else{
            entityDO.setDetail(blobDetailContent);
            return this.tenantDetailMapper.updateById(entityDO) > 0;
        }
    }
}
